﻿from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp                                        
import os
from google.appengine.api import users
from utilities import *
from entities import * 
from google.appengine.ext.webapp.util import login_required

class RegisterUser(webapp.RequestHandler):
    @login_required
    def get(self):
        template_values = {
            'header': SiteMaster.Header(self,"topnav_registerUser"),
            'footer': SiteMaster.Footer(),
            'css'   : SiteMaster.CSS(),
            'countrys': Country.Get(),
            'user': users.get_current_user(),
            'roles': Role.Get(),
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/RegisterUser.html')
        self.response.out.write(template.render(path, template_values))

    def post(self):
        user = users.get_current_user()
        fullname = self.request.get('First') + ' '+ self.request.get('Last')
        mail = self.request.get('Email')
        mobile = self.request.get('Mobile')
        roleID = int(self.request.get('Role'))
        
        if roleID == Role.GetID("Customer"):
            avatar = str(self.request.get('img'))
            status = User.Add(user,fullname,mail,mobile,roleID)
            userID = User.GetID(user)
            status = Customer.Add(avatar,userID)
        else:
            address1 = self.request.get('Addrees1')
            address2 = self.request.get('Addrees2')
            city = self.request.get('City')
            province = self.request.get('Province')
            postal = self.request.get('Postal')
            countryID = int(self.request.get('Country'))
            company = self.request.get('Company')
            website = self.request.get('Website')
            ppAccount = self.request.get('PaypalAccount')
            User.Add(user,fullname,mail,mobile,roleID)
            userID = User.GetID(user)
            status = Developer.Add(userID,countryID,address1,address2,city,province,postal,company,website,ppAccount)
            
        template_values = {
            'status':status,
            'header': SiteMaster.Header(self,"topnav_home"),
            'footer': SiteMaster.Footer(),
            'css'   : SiteMaster.CSS(),
           
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/RegisterResult.html')
        self.response.out.write(template.render(path, template_values))
        
        
        